package com.example.baseadapter;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class CaseDetail extends ActionBarActivity{
	
	TextView p,r,cR;
	int id1;
	DatabaseHelper db;
	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setting action bar background color
	       getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb(10,132,132)));
	       db = new DatabaseHelper(getApplicationContext());
		setContentView(R.layout.case_detail);
		
	p=(TextView)findViewById(R.id.b);
	r=(TextView)findViewById(R.id.d);
	cR=(TextView)findViewById(R.id.f);
	getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	getSupportActionBar().setHomeButtonEnabled(true);
	 id1 = getIntent().getExtras().getInt("id");
	 getCaseData();
	
	}
	
	public void getCaseData() {
		//setting case data in edittext
				Case obj = db.getSingleCase(id1 );
				p.setText(obj.getPlaintiff());
				r.setText(obj.getRespondent());
				cR.setText(obj.getCaseRefno());
				
		
	}

	public void addDateToCase(View v)
	{   
		Intent i=new Intent(this,AddDate.class);
		//transfering data to the AddDate class through intent function putExtra.
	i.putExtra("caseId",id1);
		startActivity(i);
	}
	
	
	public void deleteCase(View v)
	{
     
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				CaseDetail.this);

		// set title
		alertDialogBuilder.setTitle("Do you want to delete the case??");

		// set dialog message
		alertDialogBuilder
				.setMessage("Click yes to delete!")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								// if this button is clicked, close
								// current activity
								deleteCaseFromDatabase();
								
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// if this button is clicked, just close
						// the dialog box and do nothing
						dialog.cancel();
						
					}
				});

		// create alert dialog
		AlertDialog alertDialog = alertDialogBuilder.create();

		// show it
		alertDialog.show();
	}
	
	
	private void deleteCaseFromDatabase() {
	
		db.deleteCaseHelper(id1);
		Toast.makeText(this,"Case has been deleted successfully",Toast.LENGTH_LONG).show();
		Intent i=new Intent(this,MainActivity.class);
		startActivity(i);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.casedetail_menu, menu);

		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * On selecting action bar icons
	 * */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		

		// Take appropriate action for each action item click
		switch (item.getItemId()) {
		case R.id.action_edit:
			// adding new icon in the action bar
			Intent i=new Intent(this,EditCase.class);
			i.putExtra("caseId",id1);
			startActivity(i);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}

	}

	@Override
	public void onBackPressed()
	{
		
	}

	
	
}
